package com.crrcdt.vue_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crrcdt.vue_project.entity.Manager;
import com.crrcdt.vue_project.entity.Role;
import com.crrcdt.vue_project.result.Result;
import com.crrcdt.vue_project.result.ResultFactory;
import com.crrcdt.vue_project.service.ManagerService;
import com.crrcdt.vue_project.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;

/**
 * @author lj on 2020/10/22.
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/private/v1")
@Slf4j
public class UserController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private RoleService roleService;
    @GetMapping("/users")
    public Result getAllUsers(@RequestParam(name = "query", required = false) String query,
                              @RequestParam("pagenum") Long pagenum,
                              @RequestParam("pagesize") Long pagesize) {
        Page<Manager> page = new Page<Manager>(pagenum, pagesize);
        final IPage<Manager> managerIPage;
        if(query!=null){
             managerIPage = managerService.page(page, new QueryWrapper<Manager>().like("mg_name",query));
        }else {
            managerIPage = managerService.page(page, null);
        }
//        System.out.println(managerIPage);
        managerIPage.getRecords().stream().forEach(entity->{
            final Integer roleId = entity.getRoleId();
            final Role role = roleService.getById(roleId);
            entity.setRoleName(role.getRoleName());
        });
        HashMap<Object, Object> data = new HashMap<>();
        data.put("total", managerIPage.getTotal());
        data.put("pagenum", managerIPage.getCurrent());
        data.put("users", managerIPage.getRecords());
        return ResultFactory.buildSuccessResult(data, "获取成功");
    }

    //添加用户
    @PostMapping("/users")
    public Result addUser(@RequestBody Manager manager) {
        manager.setMgTime((new Date().getTime()));
        final boolean res = managerService.save(manager);
        if (res) {
            return ResultFactory.buildSuccessResult(manager, "添加成功");
        }
        return ResultFactory.buildFailResult("添加失败！");
    }

    //    修改用户状态
    @PutMapping("/users/{uId}/state/{type}")
    public Result updateUserState(@PathVariable("uId") int uId, @PathVariable("type") int type) {
        final Manager manager = new Manager();
        manager.setMgId(uId);
        manager.setMgState(type);
        final boolean b = managerService.updateById(manager);
        if (b) {
            final Manager managerNew = managerService.getById(uId);
            return ResultFactory.buildSuccessResult(managerNew, "编辑成功");
        }
        return ResultFactory.buildFailResult("编辑失败！");
    }

    // 根据 ID 查询用户信息
    @GetMapping("/users/{id}")
    public Result getUserById(@PathVariable("id") int uId) {
        final Manager manager = managerService.getById(uId);
        if (manager != null) {
            return ResultFactory.buildSuccessResult(manager, "查询成功");
        }
        return ResultFactory.buildFailResult("添加失败！");
    }

    //    编辑用户提交
    @PutMapping("/users/{id}")
    public Result UpdateUserById(@PathVariable("id") int uId,@RequestBody Manager manager) {
        manager.setMgId(uId);
        final boolean res = managerService.updateById(manager);
        if (res) {
            final Manager managerNew = managerService.getById(uId);
            return ResultFactory.buildSuccessResult(managerNew, "编辑用户成功");
        }
        return ResultFactory.buildFailResult("编辑用户失败！");
    }
    //  删除单个用户
    @DeleteMapping("/users/{id}")
    public Result DeleteUserById(@PathVariable("id") int uId) {
        final boolean res = managerService.removeById(uId);
        if (res) {
            return ResultFactory.buildSuccessResult(null, "删除用户成功");
        }
        return ResultFactory.buildFailResult("删除用户失败！");
    }
    // 分配用户角色
    @PutMapping("/users/{id}/role")
    public Result GantUserRole(@PathVariable("id") int uId,@RequestParam("rid") int rid) {
        final Manager manager = new Manager();
        manager.setMgId(uId);
        manager.setRoleId(rid);
        final boolean res = managerService.updateById(manager);
        if (res) {
            final Manager managerNew = managerService.getById(uId);
            return ResultFactory.buildSuccessResult(managerNew, "分配用户角色成功");
        }
        return ResultFactory.buildFailResult("分配用户角色失败！");
    }
}
